#!/usr/bin/env coffee

import fetch from 'node-fetch'
import dayjs from 'dayjs'

dump = (begin, end)=>
  month = begin[...7]
  r = await fetch("https://b.keruyun.com/mind/tradeManage/queryBeforeExport/extra", {
  "headers": {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,zh-HK;q=0.7,en;q=0.6",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "page-url": "https://b.keruyun.com/mind-ui/#/tradeManage/tradeOffLineExport",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-requested-with": "XMLHttpRequest",
    "cookie": "SESSION=69bc57dc-19dc-49db-b62f-2b80bcb3cd23; login_info=WGDnW/dpZ/nnEathasdRu4KelXSa7mQZuYuD6U7u+5nOJVcd3k0uqANxEj2Lzwdz+DumTtYqS91NkuDmU89wVm34t7KRsHoVUWu8w28oKzIgEVvK7hqvWO1LhnYxti5ZePnu/8Zq5aIdJGGgUGGWkJFgMxVdTVSEu7OMqWqVYHU=; kry-api-brand-id=146909; is-new-auth=true; _dvs=0:khq11uoj:6~FsUe_0zZe220SHkT4ONSDHs47PYKQi; _dvp=0:khq11uoj:SCpoeNnMwcBAtte76AzjPQXXeqIWvh9u; DISTINCE_ID=f700320a9f77292c22d6ea8f79c912d1; current_version=6; kry-corgid=146909; current_version=6; loginOrg=1; UM_distinctid=175e4dcafc76a6-041ee93b8b66a3-326f7907-13c680-175e4dcafc89e8; CNZZDATA1278057902=1593299083-1605860783-https%253A%252F%252Fb.keruyun.com%252F%7C1605860783; crw_uid=413ea085c793a89b8798c7a094c4c5e7cbd2cbcb9eab; routId=d8c9b90bd0ff4597b11811cfad87405d; kry-api-brand-id=146909; is-new-auth=true; cid=bid_146909; crw_bid=e96148abc9a0ad958f97; crw_cid=e96148abc9a0ad958f97; language=zh_CN; c_version=6; Hm_lvt_887cf26826dd888eebe2769131c9969d=1605862473; Hm_lpvt_887cf26826dd888eebe2769131c9969d=1605862473; SERVERID=5c4f16a6d2a5a8ad26a4ee0280b4ab48|1605863956|1605862469"
  },
  "referrer": "https://b.keruyun.com/mind-ui/",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "tradeStatus=&fuzzyQueryType=1&mobile=&source=&deliveryType=4%2C1%2C2%2C3%2C15&startDate=#{begin}&endDate=#{end}&orderDateType=1&brandId=146909&shopId=810380248%2C810380249%2C810380250%2C810380251%2C810380252%2C810436008%2C810436009%2C810447022%2C810447023%2C810447024%2C810478077%2C810478079%2C810478080%2C810478081%2C810478082%2C810478083%2C810537810%2C810537815%2C810569651%2C810607818%2C810670619%2C810696495%2C810714515%2C810720391%2C810720392%2C810720393%2C810720394%2C810720395%2C810720396%2C810750961%2C810750962%2C810750963%2C810750964%2C810754383%2C810754384%2C810756642%2C810760064%2C810771766%2C810771767%2C810797302%2C810797303%2C810797304%2C810811645%2C810820960%2C810820961%2C810822471%2C810824496%2C810826916%2C810842815%2C810842816%2C810846412%2C810849831%2C810849833%2C810859105%2C810859106%2C810864255%2C810866050%2C810866052%2C810866053%2C810866054%2C810866055%2C810866056%2C810866057%2C810866058%2C810866059%2C810870109%2C810876792%2C810876793%2C810876794%2C810888777%2C810899665%2C810899666%2C810899667%2C810899669%2C810899668%2C810899670%2C810899671%2C810899672%2C810899673%2C810899675%2C810899674%2C810899677%2C810899676%2C810899678%2C810899680%2C810899681%2C810899682%2C810899684%2C810899683%2C810899685%2C810899687%2C810899686%2C810899689%2C810899688%2C810899690%2C810899692%2C810899691%2C810899693%2C810899695%2C810899694%2C810965213%2C810965214%2C810965215%2C810965216%2C810965217%2C810965218%2C810965219%2C810965220%2C810965222%2C810965221%2C810965223%2C810965224%2C810965226%2C810965225%2C810965227%2C810965228%2C810965229%2C810965230%2C810965231%2C810965232%2C810965233%2C810965234%2C810965236%2C810965235%2C810965238%2C810965237%2C810965239%2C810965240%2C810965241%2C810965242%2C810965243%2C810965245%2C810965247%2C810965250%2C810965249%2C810965252%2C810965251%2C810965254%2C810965253%2C810965255%2C810965256%2C810965257%2C810965258%2C810965259%2C810965260%2C810965261%2C810965262&userName=%E6%B1%AA%E5%87%AF%EF%BC%88%E5%85%AC%E5%8F%B8%EF%BC%89&attachmentName=#{month}.xls",
  "method": "POST",
  "mode": "cors"
  })
  {data} = await r.json()

  r = await fetch("https://b.keruyun.com/mind/tradeManage/offline/downloadData", {
  "headers": {
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,zh-TW;q=0.8,zh-HK;q=0.7,en;q=0.6",
    "content-type": "application/x-www-form-urlencoded;charset=UTF-8",
    "page-url": "https://b.keruyun.com/mind-ui/#/tradeManage/tradeOffLineExport",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-origin",
    "x-requested-with": "XMLHttpRequest",
    "cookie": "SESSION=69bc57dc-19dc-49db-b62f-2b80bcb3cd23; login_info=WGDnW/dpZ/nnEathasdRu4KelXSa7mQZuYuD6U7u+5nOJVcd3k0uqANxEj2Lzwdz+DumTtYqS91NkuDmU89wVm34t7KRsHoVUWu8w28oKzIgEVvK7hqvWO1LhnYxti5ZePnu/8Zq5aIdJGGgUGGWkJFgMxVdTVSEu7OMqWqVYHU=; kry-api-brand-id=146909; is-new-auth=true; _dvs=0:khq11uoj:6~FsUe_0zZe220SHkT4ONSDHs47PYKQi; _dvp=0:khq11uoj:SCpoeNnMwcBAtte76AzjPQXXeqIWvh9u; DISTINCE_ID=f700320a9f77292c22d6ea8f79c912d1; current_version=6; kry-corgid=146909; current_version=6; loginOrg=1; UM_distinctid=175e4dcafc76a6-041ee93b8b66a3-326f7907-13c680-175e4dcafc89e8; CNZZDATA1278057902=1593299083-1605860783-https%253A%252F%252Fb.keruyun.com%252F%7C1605860783; crw_uid=413ea085c793a89b8798c7a094c4c5e7cbd2cbcb9eab; routId=d8c9b90bd0ff4597b11811cfad87405d; kry-api-brand-id=146909; is-new-auth=true; cid=bid_146909; crw_bid=e96148abc9a0ad958f97; crw_cid=e96148abc9a0ad958f97; language=zh_CN; c_version=6; Hm_lvt_887cf26826dd888eebe2769131c9969d=1605862473; Hm_lpvt_887cf26826dd888eebe2769131c9969d=1605862473; SERVERID=5c4f16a6d2a5a8ad26a4ee0280b4ab48|1605864205|1605862469"
  },
  "referrer": "https://b.keruyun.com/mind-ui/",
  "referrerPolicy": "strict-origin-when-cross-origin",
  "body": "tradeStatus=&fuzzyQueryType=1&mobile=&source=&deliveryType=4%2C1%2C2%2C3%2C15&startDate=#{begin}&endDate=#{end}&orderDateType=1&brandId=146909&shopId=810380248%2C810380249%2C810380250%2C810380251%2C810380252%2C810436008%2C810436009%2C810447022%2C810447023%2C810447024%2C810478077%2C810478079%2C810478080%2C810478081%2C810478082%2C810478083%2C810537810%2C810537815%2C810569651%2C810607818%2C810670619%2C810696495%2C810714515%2C810720391%2C810720392%2C810720393%2C810720394%2C810720395%2C810720396%2C810750961%2C810750962%2C810750963%2C810750964%2C810754383%2C810754384%2C810756642%2C810760064%2C810771766%2C810771767%2C810797302%2C810797303%2C810797304%2C810811645%2C810820960%2C810820961%2C810822471%2C810824496%2C810826916%2C810842815%2C810842816%2C810846412%2C810849831%2C810849833%2C810859105%2C810859106%2C810864255%2C810866050%2C810866052%2C810866053%2C810866054%2C810866055%2C810866056%2C810866057%2C810866058%2C810866059%2C810870109%2C810876792%2C810876793%2C810876794%2C810888777%2C810899665%2C810899666%2C810899667%2C810899669%2C810899668%2C810899670%2C810899671%2C810899672%2C810899673%2C810899675%2C810899674%2C810899677%2C810899676%2C810899678%2C810899680%2C810899681%2C810899682%2C810899684%2C810899683%2C810899685%2C810899687%2C810899686%2C810899689%2C810899688%2C810899690%2C810899692%2C810899691%2C810899693%2C810899695%2C810899694%2C810965213%2C810965214%2C810965215%2C810965216%2C810965217%2C810965218%2C810965219%2C810965220%2C810965222%2C810965221%2C810965223%2C810965224%2C810965226%2C810965225%2C810965227%2C810965228%2C810965229%2C810965230%2C810965231%2C810965232%2C810965233%2C810965234%2C810965236%2C810965235%2C810965238%2C810965237%2C810965239%2C810965240%2C810965241%2C810965242%2C810965243%2C810965245%2C810965247%2C810965250%2C810965249%2C810965252%2C810965251%2C810965254%2C810965253%2C810965255%2C810965256%2C810965257%2C810965258%2C810965259%2C810965260%2C810965261%2C810965262&userName=%E6%B1%AA%E5%87%AF%EF%BC%88%E5%85%AC%E5%8F%B8%EF%BC%89&attachmentName=#{month}.xls&totalCount=#{data}",
  "method": "POST",
  "mode": "cors"
   })
  r.json()


do =>
  YMD = 'YYYY-MM-DD'
  ymd = dayjs "2019-04-01"
  now = dayjs().format(YMD)
  running = true
  n = 0
  while running
    begin = ymd.format(YMD)
    end = ymd.endOf('month').format(YMD)
    running = end <= now
    if not running
      end = now
    console.log ++n, begin, begin[...7], end
    # console.log await dump(begin, end)
    ymd = ymd.add(1,'month')
  process.exit()
